/*********************
** Basic operations **
**********************/

function fillList( listId, contents ) {
	var list = document.getElementById( listId );
	removeChildren( list );

	for ( var i = 0; i < contents.length; i++ ) {
		$("#" + listId).append("<li><h3>"+ contents[i] +"</h3></li>");
	}
}

function fillSelect( selectId, contents ) {
	var select = document.getElementById( selectId );
	removeChildren( select );

	for ( var i = 0; i < contents.length; i++ ) {
	   var splittedName = contents[i].split("|");
	   addOptionToSelect( select, splittedName[0], splittedName[1], false, "optionSelectBoxCommonInterestsPeopleToAdd");
	}
}

function fillSelectFull( selectId, contents ) {
	var select = document.getElementById( selectId );
	removeChildren( select );

	for ( var i = 0; i < contents.length; i++ ) {
	   var splittedName = contents[i].split("|");
	   addOptionToSelect( select, splittedName[0], splittedName[1], false, "optionSelectBoxCommonInterestsPeopleToAddFull");
	}
}

/*Removes all the elements that are a child of 'node'.*/
function removeChildren(node) {
	if( node == undefined || node == null ){
		return;
	}

	while (node.hasChildNodes()) {
		node.removeChild(node.firstChild);
	}
}

/*******************************
** Operations for select tags **
********************************/

/*Add an option field to 'SelectBox'*/
function addOptionToSelect( selectBox, text, value, isSelected, className )  {
    if (selectBox != null && selectBox.options != null ) {
        selectBox.options[ selectBox.options.length ] = new Option( text, value, false, isSelected );
	    selectBox.options[ selectBox.options.length - 1 ].setAttribute("class", className );
    }
}

/*Returns a list that contains all the values of the options of the
specified className.*/
function getAllValuesInSelectByClassName( classNameOptions ) {
	var result = new Array();

	$("." + classNameOptions ).each(function() {
		result.push( $(this).text() );
	});

	return result;
}

/*This function should be called when you want to add a person to the pool
of people from which the common interest will be calculated. The common interest 
are automatically updated when you call this function.*/
function addName(){
	var selectBoxPeopleToAdd = document.getElementById("selectBoxCommonInterestsPeopleToAdd");
	var selectBoxAddedPeople = document.getElementById("selectBoxCommonInterestsAddedPeople");

	var selectedValue = selectBoxPeopleToAdd.options[ selectBoxPeopleToAdd.selectedIndex ].value;
	var selectedName = selectBoxPeopleToAdd.options[ selectBoxPeopleToAdd.selectedIndex ].text;

	if ( !contains( getAllValuesInSelectByClassName( "optionSelectBoxCommonInterestsAddedPeople" ), selectedName ) )
		addOptionToSelect( selectBoxAddedPeople, selectedName, selectedValue, false, "optionSelectBoxCommonInterestsAddedPeople" );

	updateCommonInterests();	
}

function addNameFull(){
	var selectBoxPeopleToAdd = document.getElementById("selectBoxCommonInterestsPeopleToAddFull");
	var selectBoxAddedPeople = document.getElementById("selectBoxCommonInterestsAddedPeopleFull");

	var selectedValue = selectBoxPeopleToAdd.options[ selectBoxPeopleToAdd.selectedIndex ].value;
	var selectedName = selectBoxPeopleToAdd.options[ selectBoxPeopleToAdd.selectedIndex ].text;

	if ( !contains( getAllValuesInSelectByClassName( "optionSelectBoxCommonInterestsAddedPeopleFull" ), selectedName ) )
		addOptionToSelect( selectBoxAddedPeople, selectedName, selectedValue, false, "optionSelectBoxCommonInterestsAddedPeopleFull" );

	updateCommonInterestsFull();	
}

function getSelectedValueSelectBox( selectBoxId ) {
	var selectBox = document.getElementById( selectBoxId );
	return selectBox.options[ selectBox.selectedIndex ].value;
}

/*This function should be called when you want to remove a person from the pool
of people from which the common interest will be calculated. The common interest 
are automatically updated when you call this function.*/
function removeName(){
	var selectBoxAddedPeople = document.getElementById("selectBoxCommonInterestsAddedPeople");
	selectBoxAddedPeople.remove( selectBoxAddedPeople.selectedIndex );
	updateCommonInterests();
}

function removeNameFull(){
	var selectBoxAddedPeople = document.getElementById("selectBoxCommonInterestsAddedPeopleFull");
	selectBoxAddedPeople.remove( selectBoxAddedPeople.selectedIndex );
	updateCommonInterestsFull();
}

/*This function recalulates the common interests of all the people currently in the 
pool of selected people. It will also automatically show the results in the table
'tableCommonInterests'.*/
function updateCommonInterests(){ 
    var first = true;
	var result = new Array();

	$("." + "optionSelectBoxCommonInterestsAddedPeople" ).each(function() {
	   if ( first ) {
	       first = false;
	       var topicsToAdd = $(this).val().split(", ");
	       for ( var i = 0; i < topicsToAdd.length; i++ ) {
	           result.push( topicsToAdd[i] );
	       }
	   } else {
	       var newTopics = $(this).val().split(", ");
	       result = calculateIntersection( result, newTopics );
	   }
	});
	
	if ( result.length == 0 ) {
	   result.push( "Geen onderwerpen beschikbaar" );
	}
	
	fillList( "listCommonInterests", result );
}

function updateCommonInterestsFull(){ 
    var first = true;
	var result = new Array();

	$("." + "optionSelectBoxCommonInterestsAddedPeopleFull" ).each(function() {
	   if ( first ) {
	       first = false;
	       var topicsToAdd = $(this).val().split("; ");
	       for ( var i = 0; i < topicsToAdd.length; i++ ) {
	           result.push( topicsToAdd[i] );
	       }
	   } else {
	       var newTopics = $(this).val().split("; ");
	       result = calculateIntersection( result, newTopics );
	   }
	});
	
	if ( result.length == 0 ) {
	   result.push( "Geen onderwerpen beschikbaar" );
	}
	
	fillList( "listCommonInterestsFull", result );
}


/*Return true if 'list' contains at least one element with the value 'value',
otherwise false will be returned.*/
function contains( list, value ) {
	for ( var i = 0; i < list.length; i++ ) {
		if ( list[i] == value ) {
			return true;
		}
	}
	
	return false;
}

/*Returns a list that contains all the elements that are contained in
both 'a' and 'b'. This function assumes that both 'a' and 'b' are
sorted in lexicographical order.*/
function calculateIntersection(a, b) {
	var i = 0;
	var j = 0;
	var k = 0;
	var result = new Array();

	while( i < a.length && j < b.length ) {
		if ( a[i] < b[j] ) {
			i++;
		} else if ( a[i] > b[j] ) {
			j++;
		} else {
			result[k] = a[i];
			i++;
			j++;
			k++;
		}
  	}

	return result;
}
